package com.sgb.kjwl.viewmodel.manage;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.RegexUtils;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.UrlConfig;
import com.sgb.kjwl.api.service.WebsiteInfoAPI;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.manage.UpdataIPDetailEntity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 知识产权
 */
public class IntellectualPropertyModel extends BaseViewModel {

    //知识产权详情
    private MutableLiveData<UpdataIPDetailEntity> ipDetailEntityMutableLiveData = new MutableLiveData<>(new UpdataIPDetailEntity());
    //商标分类
    private MutableLiveData<List<PickSelectEntity>> ipPickSelectList = new MutableLiveData<>(new ArrayList<>());
    //展示权限
    private MutableLiveData<List<PickSelectEntity>> permissionPickSelectList = new MutableLiveData<>(new ArrayList<>());
    //领域分类
    private MutableLiveData<List<PickSelectEntity>> fieldPickSelectList = new MutableLiveData<>(new ArrayList<>());
    //专利状态
    private MutableLiveData<List<PickSelectEntity>> patentStatusPickSelectList = new MutableLiveData<>(new ArrayList<>());

    private MutableLiveData<String> urlString = new MutableLiveData<>("");

    public MutableLiveData<UpdataIPDetailEntity> getMutableLiveData() {
        return ipDetailEntityMutableLiveData;
    }

    public MutableLiveData<List<PickSelectEntity>> getPickSelectList() {
        return ipPickSelectList;
    }

    public MutableLiveData<List<PickSelectEntity>> getPermissionPickSelectList() {
        return permissionPickSelectList;
    }

    public MutableLiveData<String> getUrlString() {
        return urlString;
    }

    public MutableLiveData<List<PickSelectEntity>> getFieldPickSelectList() {
        return fieldPickSelectList;
    }

    public MutableLiveData<List<PickSelectEntity>> getPatentStatusPickSelectList() {
        return patentStatusPickSelectList;
    }

    public IntellectualPropertyModel() {
    }

    /**
     * 获取知识产权详情
     *
     * @param id
     */
    public void getIntellectualPropertydetail(Context mActivity, String id) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getIntellectualPropertydetail(WebsiteInfoAPI.NetBuilderUrl.INTELLECTUAL_PROPERTY_DEAIL,id).enqueue(new ICallBack<BaseEntity<UpdataIPDetailEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UpdataIPDetailEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    ipDetailEntityMutableLiveData.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 提交知识产权编辑
     *
     * @param map
     */
    public void addOrUpdate(Context mActivity, Map<String, Object> map) {
        APIManager.getInstance().changeAPIManager(UrlConfig.NEW_BASE_URL);
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().addOrUpdate(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    getMessage().postValue("操作成功");
                }
            }
        });
        APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
    }

    /**
     * 获取分类
     *
     * @param title
     */
    public void getTypList(Context mActivity, String title) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        switch (title) {
            case "商标":
                getTrademarkTypeOption();
                break;
            case "专利":
                getPatentTypeOption();
                break;
            case "版权":
                getCopyrightTypeOption();
                break;
        }
    }

    /**
     * 获取商标类型
     */
    private void getTrademarkTypeOption() {
        APIManager.getInstance().getWebsiteInfoAPI().getTrademarkTypeOptionForShop().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getData() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    ipPickSelectList.postValue(data.getData());
                }
            }
        });
    }

    /*
     * 获取版权分类选项
     */
    private void getCopyrightTypeOption() {
        APIManager.getInstance().getWebsiteInfoAPI().getCopyRightTypeOptionForShop().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    ipPickSelectList.postValue(data.getData());
                }
            }
        });
    }

    /*
     * 商标权限 本地写入数据
     */
    public void getSBOption(Context mActivity) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        PickSelectEntity entity1 = new PickSelectEntity("1", "所有人可见");
        PickSelectEntity entity2 = new PickSelectEntity("2", "仅会员可见");
        PickSelectEntity entity3 = new PickSelectEntity("3", "仅自己可见");
        List<PickSelectEntity> onlyDatas = new ArrayList<>();
        onlyDatas.add(entity1);
        onlyDatas.add(entity2);
        onlyDatas.add(entity3);
        DialogHelper.dismissProgressDialog();
        permissionPickSelectList.postValue(onlyDatas);
    }

    /**
     * 获取领域分类
     */
    public void getFieldClassifyType(Context mContext) {
        DialogHelper.showProgressDialog(mContext, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getFieldClassifyType().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getData() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fieldPickSelectList.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 获取专利状态
     */
    public void getPatentStatus(Context mContext) {
        DialogHelper.showProgressDialog(mContext, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getPatentStatus().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getData() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    patentStatusPickSelectList.setValue(data.getData());
                }
            }
        });
    }

    /**
     * 专利类型
     */
    private void getPatentTypeOption() {
        APIManager.getInstance().getWebsiteInfoAPI().getPatentTypeOptionForShop().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getData() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    ipPickSelectList.postValue(data.getData());
                }
            }
        });
    }

    public void uploadAvatar(Context mContext, File file) {
        DialogHelper.showProgressDialog(mContext, null, "图片上传中...", 0, false, null).setCanceledOnTouchOutside(true);
        //创建map集合
        Map<String, RequestBody> photos = new HashMap<>();
        //将文件转换成多媒体文件，方便上传
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + RegexUtils.getReplaceAll(file.getName(), "[\u4e00-\u9fa5]", "") + "image.jpg\"", requestBody);
        //接口请求
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data == null || data.getData() == null) {
                    MToast.showToast(mContext, "上传失败");
                    return;
                }
                urlString.postValue(data.getData().toString());
            }
        });
    }
}